Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable checking for f strings #17732

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

nielsbuwen
Copy link

@nielsbuwen nielsbuwen commented Sep 3, 2024

This implements #17714

Support for f-string checking was already partially implemented. Consider this code:

f"{123:abc}"
"{:abc}".format(123)

Mypy ran these lines through the format checker. The call expressions look like this:

# f-string
CallExpr:1(
  MemberExpr:1(
    StrExpr({:{}})
    format)
  Args(
    IntExpr(123)
    StrExpr(abc)))

# format
CallExpr:2(
  MemberExpr:2(
    StrExpr({:abc})
    format)
  Args(
    IntExpr(123)))

The difference is, that the actually static "abc" is converted to a "dynamic" spec.

This MR attempts to enable type checking of f-strings by inlining these "semi dynamic" specs.

Open Questions:

  • is this a good idea? Or should the actual parsing of f-strings be changed?
  • the test i added always passes on my machine: pytest mypy/test/testcheck.py::TypeCheckSuite::check-string-format.test. It never finds any errors even when it should clearly fail

Checklist

  • Read the Contributing Guidelines
  • Add tests for all changed behavior
  • Make sure CI passes
  • Do not force push to PR once reviewed

This comment has been minimized.

Copy link
Contributor

github-actions bot commented Sep 3, 2024

Diff from mypy_primer, showing the effect of this PR on open source code:

websockets (https://github.com/aaugustin/websockets)
+ src/websockets/sync/messages.py:102: error: Incompatible types in string interpolation (expression has type "None", placeholder has type "int | float")  [str-format]

pandas (https://github.com/pandas-dev/pandas)
+ pandas/tests/scalar/test_na_scalar.py:38: error: Unrecognized format specification "xxx"  [str-format]

pwndbg (https://github.com/pwndbg/pwndbg)
+ pwndbg/gdblib/symbol.py: note: In function "get":
+ pwndbg/gdblib/symbol.py:100: error: Incompatible types in string interpolation (expression has type "Value", placeholder has type "int")  [str-format]

freqtrade (https://github.com/freqtrade/freqtrade)
+ freqtrade/persistence/trade_model.py:852: error: Incompatible types in string interpolation (expression has type "None", placeholder has type "int | float")  [str-format]
+ freqtrade/rpc/telegram.py:399: error: Incompatible types in string interpolation (expression has type "None", placeholder has type "int | float")  [str-format]
+ freqtrade/rpc/telegram.py:421: error: Incompatible types in string interpolation (expression has type "None", placeholder has type "int | float")  [str-format]
+ freqtrade/rpc/telegram.py:446: error: Incompatible types in string interpolation (expression has type "None", placeholder has type "int | float")  [str-format]
+ freqtrade/strategy/interface.py:1511: error: Incompatible types in string interpolation (expression has type "None", placeholder has type "int | float")  [str-format]

aiohttp (https://github.com/aio-libs/aiohttp)
+ aiohttp/connector.py:1164:32: error: Incompatible types in string interpolation (expression has type "None", placeholder has type "int")  [str-format]
+ aiohttp/connector.py:1164:32: note: See https://mypy.rtfd.io/en/stable/_refs.html#code-str-format for more info

materialize (https://github.com/MaterializeInc/materialize)
+ misc/python/materialize/feature_benchmark/comparator.py:56: error: Incompatible types in string interpolation (expression has type "T", placeholder has type "int | float")  [str-format]
+ misc/python/materialize/feature_benchmark/comparator.py:65: error: Incompatible types in string interpolation (expression has type "T", placeholder has type "int | float")  [str-format]

ignite (https://github.com/pytorch/ignite)
+ ignite/utils.py:134: error: Incompatible types in string interpolation (expression has type "Number", placeholder has type "int | float")  [str-format]
+ ignite/utils.py:165: error: Incompatible types in string interpolation (expression has type "Number", placeholder has type "int | float")  [str-format]

Copy link
Contributor

@A5rocks A5rocks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to mypy-issues, this PR also fixes:

(I just realized after I ran it that that issue is mentioned in 17714 but whatever.)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO this should go to test-data/unit/check-formatting.test

if spec.format_spec == ":{}": # most simple dynamic case
assert (
expression is not None
) # dynamic spec cannot be last, this should have been detected earlier
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind adding a test case? I imagine just "{:{}}".format("aaaa") would work... (I'm not sure what could possibly trigger this though)

new_format_string = f"{{{spec.conversion or ''}:{next_expression.value}}}"
parsed = parse_format_value(new_format_string, call, self.msg)
if parsed is None or len(parsed) != 1:
continue
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this happens if the resulting format string isn't a valid format string. Is that caught elsewhere, or does that deserve a self.msg.fail here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants